var config = require('../../config.js');
var common = require('../utils/common.js');
var promise = require('../utils/es6-promise.auto.min.js');
var util = require('../utils/util.js');
var app = getApp();

//装载数据
function wrapper_data(materials){
  var foramt = formateDate(materials.showTime);
  materials.showTime = foramt;
  materials.day = util.getDays(materials.showTime);
  materials.bindtap = "inplay";

  var date = new Date(foramt);
  materials.hour = date.getFullYear();
  materials.minutes = (date.getMonth() + 1) > 9 ? date.getMonth() + 1 : "0" + (date.getMonth() + 1);
  materials.seconds = date.getDate() > 9 ? date.getDate() : "0" + date.getDate();
  var lengthInSecond = materials.lengthInSecond;
  if (lengthInSecond) {
    materials.lengthInSecond = transSS(lengthInSecond);
  }
}

/**
 * data  参数
 * tab_page
 */
function loadPage(params, tab_tab_list, page) {
  wx.request({
    url: config.materials + '/search/findByTagNameAndType',
    data: params,
    header: {
      'content-type': 'application/json'
    },
    success: function (res) {
      var data = res.data;
      var templist = tab_tab_list.list;
      var materials = data._embedded.materials;
    
      new promise(function (resolve, reject) {
        for (var i = 0; i < materials.length; i++) {
          wrapper_data(materials[i]);
        }
        resolve(materials);
      }).then(function (materials) {
        //接口获取 收藏
        templist = templist.concat(materials);
        tab_tab_list.list = templist;

        page.setData({
          list: templist,
        });
      });
    }
  })

}

function load_list(params, currentPage, tab_tab_list, page) {
  var list=wx.getStorageSync("new_list");
  if(list){
    tab_tab_list=list;
    page.setData({
      list: list.list
    });
    return;
  }
  wx.request({
    url: config.materials + '/search/findByTagNameAndType',
    data: params,
    header: {
      'content-type': 'application/json'
    },
    success: function (res) {
      var data = res.data;
      var materials = data._embedded.materials;
      new promise(function (resolve, reject) {
        for (var i = 0; i < materials.length; i++) {
          wrapper_data(materials[i]);
        }
        resolve(materials);
      }).then(function (materials) {
        var templist = materials;
        tab_tab_list.list = templist;
        tab_tab_list.page = currentPage;
        tab_tab_list.pageSize = data.page.totalPages;
        wx.setStorageSync("new_list", tab_tab_list);
        page.setData({
          list: templist
        });
      });
    }
  });
}

function load_hot_list(params, currentPage, tab_tab_list, page) {
  var list = wx.getStorageSync("hot_list");
  if (list) {
    tab_tab_list= list;
    page.setData({
      list: list.list
    });
    return;
  }
  wx.request({
    url: config.materials + '/search/findByTagNameAndType',
    data: params,
    header: {
      'content-type': 'application/json'
    },
    success: function (res) {
      var data = res.data;
      var materials = data._embedded.materials;
      new promise(function (resolve, reject) {
        for (var i = 0; i < materials.length; i++) {
          wrapper_data(materials[i]);
        }
        resolve(materials);
      }).then(function (materials) {
        var templist = materials;
        tab_tab_list.list = templist;
        tab_tab_list.page = currentPage;
        tab_tab_list.pageSize = data.page.totalPages;
        wx.setStorageSync("hot_list", tab_tab_list);
        page.setData({
          list: templist
        });
      });
    }
  });
}


/**
 * 收藏或是移除收藏
 * id 
 * action 为add或remove,cb  回掉函数
 */
function add_rem_favorite(id, name, action, cb) {
  wx.request({
    url: config.materials + '/' + id + '/favor',
    data: {
      username: name,
      action: action
    },
    method: 'get',
    header: {
      'content-type': 'application/json'
    },
    success: function (res) {
      if ("success" == res.data) {
        //render_page(id,);
        cb;
      }
    }
  });
}
/**
 * id  
 * fid表示收藏或是不收藏0 或1
 */
function render_page(id, fid, tab_index, tab_new, tab_hot, tab_tj, that) {
  var new_list = tab_new.list;
  var new_favorlist = tab_new.favorlist;
  //var list=new_list;
  var list = new_favorlist;
  if (new_list != null && undefined != new_list) {
    change_favorite_list(id, fid, new_list, new_favorlist);
  }
  var hot_list = tab_hot.list;
  var hot_favorlist = tab_hot.favorlist;
  if (hot_list != null && undefined != hot_list) {
    change_favorite_list(id, fid, hot_list, hot_favorlist);
  }
  var tj_list = tab_tj.list;
  var tj_favorlist = tab_tj.favorlist;
  if (tj_list != null && undefined != tj_list) {
    change_favorite_list(id, fid, tj_list, hot_favorlist);
  }
  if (tab_index == 1) {
    list = hot_favorlist
  } else if (tab_index == 2) {
    list = tj_favorlist;
  }
  that.setData({
    favorlist:list
  });
}

function change_favorite_list(id, fid, list, favorlist) {
  for (var i = 0; i < list.length; i++) {
    if (list[i].id == id) {
      favorlist[i]=fid=='0'?false:true;
    }
  }
}
//主要是加载推荐的数据查询页处理
function load_status_page(params, tab_tab_list, page) {
  wx.request({
    url: config.materials + '/search/findByTagNameAndTypeAndStatus',
    data: params,
    header: {
      'content-type': 'application/json'
    },
    success: function (res) {
      var data = res.data;
      var templist = tab_tab_list.list;
      var materials = data._embedded.materials;
      new promise(function (resolve, reject) {
        for (var i = 0; i < materials.length; i++) {
          wrapper_data(materials[i]);
        }
        resolve(materials);
      }).then(function (materials) {
        //接口获取 收藏
        templist = templist.concat(materials);

        tab_tab_list.list = templist;

        page.setData({
          list: templist
        });
      });

    }
  })

}
function load_status_list(params, currentPage, tab_tab_list, page) {
  var list = wx.getStorageSync("status_list");
  if (list) {
    tab_tab_list= list;
    page.setData({
      list: list.list
    });
    return;
  }
  wx.request({
    url: config.materials + '/search/findByTagNameAndTypeAndStatus',
    data: params,
    header: {
      'content-type': 'application/json'
    },
    success: function (res) {
      var data = res.data;
      var materials = data._embedded.materials;
     
      new promise(function (resolve, reject) {
        for (var i = 0; i < materials.length; i++) {
          wrapper_data(materials[i]);
        }
        resolve(materials);
      
      }).then(function (materials) {
        var templist = materials;
        tab_tab_list.list = templist;
        tab_tab_list.page = currentPage;
        tab_tab_list.pageSize = data.page.totalPages;
        wx.setStorageSync("status_list", tab_tab_list);
        page.setData({
          list: templist
        });
      });
    }
  });
}
//格式化时间
function formateDate(objTime) {
  var pattern = /(\d{4})(\d{2})(\d{2})/;
  var formated = objTime.replace(pattern, '$1-$2-$3');
  return formated;
}
//获取首页首条数据
function getFirst(page) {
  var data = {
    tagName: config.tagName,
    type: 'VIDEO',
    page: 0,
    size: 1,
    sort: 'showTime,desc',
  }
  wx.request({
    url: config.materials + '/search/findByTagNameAndType',
    data: data,
    header: {
      'content-type': 'application/json'
    },
    success: function (res) {
      var data = res.data;
      var materials = data._embedded.materials;
      if (materials.length > 0) {
        var foramt = formateDate(materials[0].showTime);
        materials[0].showTime = foramt;
        materials[0].day = util.getDays(materials[0].showTime); 

        page.setData({
          title: materials[0].title,
          id: materials[0].id,
          showTime: materials[0].showTime,
          day: materials[0].day,
          coverUrl: materials[0].coverUrl
        });
      }
    }, fail: function (res) {

    }
  });
}

function getTwoHead(page){
  var data = {
    tagName: config.tagName,
    type: 'VIDEO',
    page: 0,
    size: 2,
    sort: 'showTime,desc',
  }
  wx.request({
    url: config.materials + '/search/findByTagNameAndType',
    data: data,
    header: {
      'content-type': 'application/json'
    },
    success: function (res) {
      var data = res.data;
      var materials = data._embedded.materials;
      if (materials.length > 0) {
        for(var i=0;i<materials.length;i++){
          var foramt = formateDate(materials[i].showTime);
          materials[i].showTime = foramt;
          materials[i].day = util.getDays(materials[i].showTime);
          materials[i].bindtap = "inplay";
        }

        page.setData({
          headList: materials
        });
      }
    }, fail: function (res) {

    }
  });

}
function transSS(lengthInSecond){
  var minute = parseInt(lengthInSecond/60);
  var second = parseInt(lengthInSecond-minute*60);
  var str = "0" + minute + ":" + (second > 9 ? second : '0' + second);
  return str;
}

function getTwoSeries(page){
  var data = {
    tagName: config.tagName,
    status:"QUALIFIED",
    page: 0,
    size: 3,
    sort: 'time,desc',
  }
  wx.request({
    url: config.series + '/search/findByTagNameAndStatus',
    data: data,
    header: {
      'content-type': 'application/json'
    },
    success: function (res) {
      var data = res.data;
      var series = data._embedded.series;
      for(var i=0;i<series.length;i++){
        series[i].bindtap ="inplay_series";
      }
      page.setData({
        headList: series
      });
    }
  });    

}

module.exports = {
  loadPage: loadPage,
  load_list: load_list,
  load_hot_list: load_hot_list,
  add_rem_favorite: add_rem_favorite,
  render_page: render_page,
  load_status_page: load_status_page,
  load_status_list: load_status_list,
  getFirst: getFirst,
  getTwoHead: getTwoHead,
  getTwoSeries: getTwoSeries
}